<?php
/**
 * 作者: 王志昂-黑米<wangzhiangtony@qq.com>
 * 个人说明: 春水初涨，春林初盛，春风十里，不如你。
 * 创建时间: 2015-08-19 16:18
 * 说明:
 */
namespace Home\Service;
use \Think\Model;

class ItService extends Model
{
    public function ecs($id = null)
    {
        if (empty($id)) {
            $sql = 'SELECT
                ziei.*, zu.realname,
                zu.nickname
            FROM
                zt_itoa_ecs_info AS ziei
            LEFT JOIN zt_itoa_user_admin AS ziua ON ziua.app_id = ziei.id
            LEFT JOIN zt_user AS zu ON zu.id = ziua.user_id
            WHERE
                ziua.app_type = "ecs"
            AND ziua.is_deleted = "N"
            AND ziei.is_deleted = "N"';
        } else {
            $sql = 'SELECT
                ziei.*, zu.realname,
                zu.nickname
            FROM
                zt_itoa_ecs_info AS ziei
            LEFT JOIN zt_itoa_user_admin AS ziua ON ziua.app_id = ziei.id
            LEFT JOIN zt_user AS zu ON zu.id = ziua.user_id
            WHERE
                ziua.app_type = "ecs"
            AND ziua.is_deleted = "N"
            AND ziei.is_deleted = "N"
            AND ziei.id=' . $id;
        }

        $res = M()->query($sql);
        $ecsList = array();
        foreach ($res as $value) {
            $ecsList[$value['id']]['info'] = $value;
            $ecsList[$value['id']]['users'][] = $value['realname'];
        }
        return $ecsList;
    }

    public function mysql($ecsId = null)
    {
        if (empty($ecsId)) {
            $sql = 'SELECT
                zimi.*,ziei.id as host_id,ziei.hostname
            FROM
                zt_itoa_mysql_info AS zimi
            LEFT JOIN zt_itoa_ecs_info AS ziei ON zimi.ecs_id = ziei.id
            WHERE
                zimi.is_deleted = "N" and ziei.is_deleted = "N"';
        } else {
            $sql = 'SELECT
                zimi.*,ziei.id as host_id,ziei.hostname
            FROM
                zt_itoa_mysql_info AS zimi
            LEFT JOIN zt_itoa_ecs_info AS ziei ON zimi.ecs_id = ziei.id
            WHERE
                zimi.is_deleted = "N" and ziei.is_deleted = "N" and ziei.id = ' . $ecsId;
        }

        $res = M()->query($sql);
        $mysqlList = array();
        foreach ($res as $value) {
            $mysqlList[$value['id']] = $value;
        }
        return $mysqlList;
    }

    public function nginx($ecsId = null)
    {
        if (empty($ecsId)) {
            $sql = 'SELECT
                zini.*,ziei.id as host_id,ziei.hostname
            FROM
                zt_itoa_nginx_info AS zini
            LEFT JOIN zt_itoa_ecs_info AS ziei ON zini.ecs_id = ziei.id
            WHERE
                zini.is_deleted = "N" and ziei.is_deleted = "N"';
        } else {
            $sql = 'SELECT
                zini.*,ziei.id as host_id,ziei.hostname
            FROM
                zt_itoa_nginx_info AS zini
            LEFT JOIN zt_itoa_ecs_info AS ziei ON zini.ecs_id = ziei.id
            WHERE
                zini.is_deleted = "N" and ziei.is_deleted = "N" and ziei.id = ' . $ecsId;
        }

        $res = M()->query($sql);
        $nginxList = array();
        foreach ($res as $value) {
            $nginxList[$value['id']] = $value;
        }
        return $nginxList;
    }

    public function tomcat($ecsId = null)
    {
        if (empty($ecsId)) {
            $sql = 'SELECT
                ziti.*,ziei.id as host_id,ziei.hostname
            FROM
                zt_itoa_tomcat_info AS ziti
            LEFT JOIN zt_itoa_ecs_info AS ziei ON ziti.ecs_id = ziei.id
            WHERE
                ziti.is_deleted = "N" and ziei.is_deleted = "N"';
        } else {
            $sql = 'SELECT
                ziti.*,ziei.id as host_id,ziei.hostname
            FROM
                zt_itoa_tomcat_info AS ziti
            LEFT JOIN zt_itoa_ecs_info AS ziei ON ziti.ecs_id = ziei.id
            WHERE
                ziti.is_deleted = "N" and ziei.is_deleted = "N" and ziei.id = ' . $ecsId;
        }

        $res = M()->query($sql);
        $tomcatList = array();
        foreach ($res as $value) {
            $tomcatList[$value['id']] = $value;
        }
        return $tomcatList;
    }

    public function redis($ecsId = null)
    {
        if (empty($ecsId)) {
            $sql = 'SELECT
                ziri.*, ziei.id AS host_id,
                ziei.hostname,
                zu.realname,
                zu.nickname
            FROM
                zt_itoa_user_admin AS ziua
            LEFT JOIN zt_itoa_redis_info AS ziri ON ziua.app_id = ziri.id
            LEFT JOIN zt_itoa_ecs_info AS ziei ON ziri.ecs_id = ziei.id
            LEFT JOIN zt_user AS zu ON zu.id = ziua.user_id
            WHERE
                ziua.app_type = "redis"
            AND ziua.is_deleted = "N"
            AND ziri.is_deleted = "N"
            AND ziei.is_deleted = "N"';
        } else {
            $sql = 'SELECT
                ziri.*, ziei.id AS host_id,
                ziei.hostname,
                zu.realname,
                zu.nickname
            FROM
                zt_itoa_user_admin AS ziua
            LEFT JOIN zt_itoa_redis_info AS ziri ON ziua.app_id = ziri.id
            LEFT JOIN zt_itoa_ecs_info AS ziei ON ziri.ecs_id = ziei.id
            LEFT JOIN zt_user AS zu ON zu.id = ziua.user_id
            WHERE
                ziua.app_type = "redis"
            AND ziua.is_deleted = "N"
            AND ziri.is_deleted = "N"
            AND ziei.is_deleted = "N"
            AND ziei.id = ' . $ecsId;
        }

        $res = M()->query($sql);
        $redisList = array();
        foreach ($res as $value) {
            $redisList[$value['id']]['info'] = $value;
            $redisList[$value['id']]['users'][] = $value['realname'];
        }
        return $redisList;
    }
}